package cn.oms.esb.provider.controller;

import cn.oms.esb.api.dto.req.SendEmailReq;
import cn.oms.esb.api.service.IEmailService;
import cn.oms.esb.api.dto.enums.EmailTemplateEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

// 描述一个API类
@Api(tags = "总线服务API接口")
@RestController
@RequestMapping("/email/api")
@Slf4j
public class EmailController {

    @Resource
    private IEmailService emailService;


    /**
     * 发送邮件
     */
    // 发送邮件API的方法描述
    @ApiOperation(value="发送邮件", notes="发送邮件", produces="application/json")
    // 发送邮件API的一组请求参数的描述
    @ApiImplicitParams({
            //  发送邮件API的一个请求参数的描述
            @ApiImplicitParam(name = "title",value = "邮件标题",required = true,dataType = "String"),
            @ApiImplicitParam(name = "content",value = "邮件正文",required = true,dataType = "String"),
            @ApiImplicitParam(name = "to",value = "收件人",required = true,dataType = "String")
    })
    @RequestMapping(value="/sendEmail", method= RequestMethod.POST)
    public String sendEmail(SendEmailReq emailReq) {
        log.info("调用了邮件发送接口");
        log.info("收件人：{},标题：{},内容：{},",emailReq.getTo(),emailReq.getTitle(),emailReq.getContent());

        String msg = emailService.sendEmail(emailReq);
        if ("SUCCESS".equals(msg)){
            return "1";
        }
        return "2";
    }


    /**
     * 发送邮箱验证码
     *
     * @param email
     * @return
     */
    @RequestMapping(value="/getVerificationCode?{email}")
    public String getVerificationCode(@PathVariable String email) {
        log.info("调用了邮箱发送邮箱验证码接口,邮箱已发送至{}",email);

        String msg = emailService.sendVerificationCode(email, EmailTemplateEnum.EmailTemplateType.VERIFICATION.getCode());
        if ("SUCCESS".equals(msg)){
            return "1";
        }
        return "2";
    }


    /**
     * 验证验证码是否正确
     *
     * @param verificationCode
     * @return
     */
    //要想获得占位符的值必须满足以下条件
    // 1.声明的参数前加@PathVariable注解
    // 2.参数名称必须和{}中占位符一致
    @RequestMapping(value="/checkEmailVerificationCode/{verificationCode}")
    public String checkEmailVerificationCode(@PathVariable String verificationCode) {
        log.info("调用了查询邮箱验证码接口");
        return "true";
    }


}
